/**
 * Copyright 2009 University of  Washington
 * This code is released under the GPL version 3 - with a copy of the license included
 * @author Maya Hao Li
 *
 */
package seedpod.rdb;

import seedpod.exceptions.SeedpodException;
import seedpod.model.NoObjectFoundException;
import seedpod.model.Thing;

public class QueryFactory {

	protected static final String STAR = " * ";

	private QueryFactory() {}

	public static String getObjectType( int oid ) throws NoObjectFoundException  {
		try {
			return Thing.getClassType( PMFactory.getInstance().createManager(), oid );
		}
		catch (SeedpodException e) {
			throw new NoObjectFoundException();
		}
	}

	public static StringBuffer selectQuery( String tableName, String[] columns, String condition ) {
		StringBuffer sql = new StringBuffer();
		sql.append( RdbSql.SELECT );
		if ( columns != null && columns.length != 0 ) {
			for ( int i = 0; i < columns.length; i++ ) {
				if ( i > 0 )
					sql.append( (SqlUtil.C ) );
				sql.append( SqlUtil.QQ( columns[i] ) );
			}
		} else
			sql.append( STAR );
		
		sql.append( RdbSql.FROM );
		sql.append( SqlUtil.QQ( tableName ) );

		if ( condition != null && condition.length() > 0 ) {
			sql.append( RdbSql.WHERE );
			sql.append( condition );
		}
		
		sql.append( SqlUtil.SC );
		return sql;
	}
	
}
